package stdio.costa.imoca.servlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import stdio.costa.imoca.notification.sms.SMSResult;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.core.util.MultivaluedMapImpl;

@Controller
@RequestMapping(value = "/sms")
public class SMSController extends BaseController {

	@RequestMapping(value = "/send")
	public String add(final HttpServletRequest request,
			final HttpServletResponse response, final ModelMap result)
			throws Exception {

		String accessToken = (String) request.getSession(false).getAttribute(
				"access_token");
		String mobile = request.getParameter("mobile");
		String content = request.getParameter("content");

		ClientConfig config = new DefaultClientConfig();
		Client client = Client.create(config);
		WebResource service = client.resource(getBaseURI());

		MultivaluedMap<String, String> param = new MultivaluedMapImpl();
		param.add("mobile", mobile);
		param.add("content", content);
		param.add("access_token", accessToken);

		SMSResult smsResult = service.path("sms").path("send")
				.queryParams(param).type(MediaType.APPLICATION_FORM_URLENCODED)
				.accept(MediaType.APPLICATION_JSON).post(SMSResult.class);

		System.out.println(smsResult);
		
		result.put("smsResult", smsResult);

		return "send_sms_result";
	}

}
